package github.kawaiior.yuanchuxiuzhen.common.item.misc;

import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.gongfa.IGongFa;
import github.kawaiior.yuanchuxiuzhen.core.init.itemgroup.YcxzGroup;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.common.util.NonNullConsumer;

public class GongFaItem extends Item {

    private final IGongFa gongFa;

    public GongFaItem(IGongFa gongFa) {
        super(new Properties().group(YcxzGroup.YCXZ_GROUP));
        this.gongFa = gongFa;
    }

    private void handleGongFa(IGongFa gf, PlayerEntity player, World world, ManaPower power, ItemStack stack){
        if (power.canLearnGongFa(gf)) {
            stack.shrink(1);
            power.setLearningGongFa(gf);
            gf.sendLearningMessage(player);
            gf.onPlayerLearn(player, world, power);
        }else {
            ManaPower.LearnGongFanEnum gongFanEnum = power.getGongFaEnum(gf);

            if (gongFanEnum == ManaPower.LearnGongFanEnum.LEARNED){
                player.sendMessage(new StringTextComponent("已习得此功法"), player.getUniqueID());
            }
            else if (gongFanEnum == ManaPower.LearnGongFanEnum.NON_MANA_ROOT){
                player.sendMessage(new StringTextComponent("缺少灵根"), player.getUniqueID());
            }
            else if (gongFanEnum == ManaPower.LearnGongFanEnum.NON_PRE){
                player.sendMessage(new StringTextComponent("需要学习前置功法"), player.getUniqueID());
            }
            else if (gongFanEnum == ManaPower.LearnGongFanEnum.LEARNING_OTHER){
                player.sendMessage(new StringTextComponent("正在学习其他功法"), player.getUniqueID());
            }
            else if (gongFanEnum == ManaPower.LearnGongFanEnum.LEARNING){
                player.sendMessage(new StringTextComponent("正在学习此功法"), player.getUniqueID());
            }
        }
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
        if (!world.isRemote){
            LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
            capability.ifPresent((power)->{
                ItemStack stack = player.getHeldItem(hand);
                Item item = stack.getItem();
                if (item instanceof GongFaItem){
                    IGongFa gf = ((GongFaItem)item).getGongFa();
                    handleGongFa(gf, player, world, power, stack);
                }
            });
        }
        return super.onItemRightClick(world, player, hand);
    }

    public IGongFa getGongFa() {
        return gongFa;
    }
}
